package com.ningmengs789.live.live.live.play;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ningmengs789.live.Const;
import com.ningmengs789.live.Interface.ALCallback;
import com.ningmengs789.live.R;
import com.ningmengs789.live.http.JsonBuilder;
import com.ningmengs789.live.http.OKHttpUtils;
import com.ningmengs789.live.live.BaseSockActivity;
import com.ningmengs789.live.live.IMLVBLiveRoomListener;
import com.ningmengs789.live.live.commondef.AnchorInfo;
import com.ningmengs789.live.live.commondef.AttributeInfo;
import com.ningmengs789.live.live.commondef.MLVBCommonDef;
import com.ningmengs789.live.live.live.common.utils.DialogUitl;
import com.ningmengs789.live.live.live.common.widget.LoadingView;
import com.ningmengs789.live.live.live.common.widget.beautysetting.BeautyLvJingFragment;
import com.ningmengs789.live.live.live.common.widget.beautysetting.BeautyTieZhiFragment;
import com.ningmengs789.live.live.live.common.widget.beautysetting.IBeauty;
import com.ningmengs789.live.live.live.common.widget.beautysetting.TCBeautyHelperNew;
import com.ningmengs789.live.live.live.common.utils.TCConstants;
import com.ningmengs789.live.live.live.common.utils.TCFrequeControl;
import com.ningmengs789.live.live.live.common.utils.TCUtils;
import com.ningmengs789.live.live.live.common.widget.TCInputTextMsgDialog;
import com.ningmengs789.live.live.live.common.widget.TCSwipeAnimationController;
import com.ningmengs789.live.live.live.common.widget.TCUserAvatarListAdapter;
import com.ningmengs789.live.live.live.common.widget.TCVideoWidget;
import com.ningmengs789.live.live.live.common.widget.TCVideoWidgetList;
import com.ningmengs789.live.live.live.common.widget.beautysetting.VideoMaterialMetaData;
import com.ningmengs789.live.live.live.common.widget.danmaku.DanmuBean;
import com.ningmengs789.live.live.live.common.widget.danmaku.LiveDanmuPresenter;
import com.ningmengs789.live.live.live.common.widget.gift.LiveGiftDialogFragment;
import com.ningmengs789.live.live.live.common.widget.gift.bean.LiveReceiveGiftBean;
import com.ningmengs789.live.live.live.common.widget.gift.bean.LiveReceiveGiftBeanItem;
import com.ningmengs789.live.live.live.common.widget.gift.utils.OnItemClickListener;
import com.ningmengs789.live.live.live.common.widget.gift.view.LiveRoomViewHolder;
import com.ningmengs789.live.live.live.common.widget.other.ContributeListFragment;
import com.ningmengs789.live.live.live.common.widget.other.LiveGuardBuyDialogFragment;
import com.ningmengs789.live.live.live.common.widget.other.LiveGuardDialogFragment;
import com.ningmengs789.live.live.live.common.widget.other.LiveUserDialogFragment;
import com.ningmengs789.live.live.live.common.widget.other.ManagerDialogFragment;
import com.ningmengs789.live.live.live.common.widget.other.RotaryTableFragment;
import com.ningmengs789.live.live.live.common.widget.other.ShopDialogFragment;
import com.ningmengs789.live.live.live.common.widget.other.SuperManagerDialogFragment;
import com.ningmengs789.live.live.live.common.widget.like.TCHeartLayout;
import com.ningmengs789.live.live.live.common.widget.pk.LiveLinkMicPkPresenter;
import com.ningmengs789.live.live.live.common.widget.prompt.LivePromptBean;
import com.ningmengs789.live.live.live.common.widget.prompt.LivePromptPresenter;
import com.ningmengs789.live.live.live.common.widget.red.HistoryRPDialogFragment;
import com.ningmengs789.live.live.live.common.widget.red.RPResultsDialogFragment;
import com.ningmengs789.live.live.live.common.widget.red.RobRedPacketDialogFragment;
import com.ningmengs789.live.live.live.common.widget.red.SendRedPacketDialogFragment;
import com.ningmengs789.live.live.live.list.TCChatEntity;
import com.ningmengs789.live.live.live.list.TCChatMsgListAdapter;
import com.ningmengs789.live.live.live.list.TCSimpleUserInfo;
import com.ningmengs789.live.live.response.JoinLiveResponse;
import com.ningmengs789.live.live.response.LiveViewerResponse;
import com.ningmengs789.live.live.response.LivingListResponse;
import com.ningmengs789.live.live.response.MegGift;
import com.ningmengs789.live.live.response.MegUserInfo;
import com.ningmengs789.live.live.response.UserInfo4LiveResponse;
import com.ningmengs789.live.message.Foreground;
import com.ningmengs789.live.message.db.IMConversation;
import com.ningmengs789.live.message.ui.MessageListActivity;
import com.ningmengs789.live.net.network.http.HttpException;
import com.ningmengs789.live.net.response.NoticeResponse;
import com.ningmengs789.live.net.response.PromDataResponse;
import com.ningmengs789.live.net.utils.NLog;
import com.ningmengs789.live.net.utils.NToast;
import com.ningmengs789.live.net.utils.json.JsonMananger;
import com.ningmengs789.live.socket.SocketUtil;
import com.ningmengs789.live.ui.activity.MyWalletActivity1;
import com.ningmengs789.live.ui.widget.VerticalViewPager;
import com.ningmengs789.live.utils.CommonUtils;
import com.ningmengs789.live.utils.UserInfoUtil;
import com.bumptech.glide.Glide;
import com.faceunity.FURenderer;
import com.faceunity.entity.Filter;
import com.opensource.svgaplayer.SVGAImageView;
import com.tencent.rtmp.TXLiveConstants;
import com.tencent.rtmp.ui.TXCloudVideoView;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import pl.droidsonroids.gif.GifImageView;


public class TCLivePlayerActivity extends BaseSockActivity implements
        IMLVBLiveRoomListener, View.OnClickListener,
        TCInputTextMsgDialog.OnTextSendListener,
        OnItemClickListener<Integer>,
        AdapterView.OnItemClickListener,
        IBeauty,
        FURenderer.OnTrackingStatusChangedListener,
        ALCallback
{

    private static final String TAG = "TAGTAG";

    protected TXCloudVideoView mTXCloudVideoView;
    private TCInputTextMsgDialog mInputTextMsgDialog;
    private ListView mListViewMsg;

    private ArrayList<TCChatEntity> mArrayListChatEntity = new ArrayList<>();
    private TCChatMsgListAdapter mChatMsgListAdapter;

    private TextView mMemberCount;

    private boolean mPlaying = false;

    protected String mUserId = "";
    protected String mNickname = "";
    protected String mAvatar = "";

    //头像列表控件
    private RecyclerView mUserAvatarList;
    private TCUserAvatarListAdapter mAvatarListAdapter;

    //点赞动画
    private TCHeartLayout mHeartLayout;
    //点赞频率控制
    private TCFrequeControl mLikeFrequeControl;

    //弹幕
    private LiveDanmuPresenter mLiveDanmuPresenter;
    private LivePromptPresenter mLivePromptPresenter;

    //手势动画
    private TCSwipeAnimationController mTCSwipeAnimationController;
    protected FrameLayout mBgImageView;

    private SHARE_MEDIA mShare_meidia = SHARE_MEDIA.WEIXIN;

    //连麦相关
    private static final long LINKMIC_INTERVAL = 3*1000;

    private boolean             mIsBeingLinkMic         = false;

    private ImageView mBtnLinkMic;
    private Button mBtnSwitchCamera;
    private Button mBtnBeauty;
    private Button mBtnPatrol;
    private TextView mFollowTv;
    private RelativeLayout rlPlayRoot;

    private ImageView mHeadIcon;
    private TextView mBroadcastingName;
    private TextView mHostID;

    private long                mLastLinkMicTime = 0;
    private List<AnchorInfo> mPusherList         = new ArrayList<>();
    private TCVideoWidgetList mPlayerList;

    //美颜
    private TCBeautyHelperNew mBeautyHepler;

    private boolean isHeart = false;

    private LiveLinkMicPkPresenter mLiveLinkMicPkPresenter;//主播与主播PK逻辑
    private int mWidthPixels;
    private int mTopMargin;
    private FURenderer mFURenderer;

    private LiveRoomViewHolder mLiveRoomViewHolder;

    private ViewGroup mRootContainer;
    private ArrayList<LivingListResponse.LiveListBean> mList;
    private int currentPosition;
    private VerticalViewPager mViewPager;
    private LoadingView mLoadView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        mWidthPixels = CommonUtils.getScreenWidth(this);
        mTopMargin = CommonUtils.dip2px(mContext, 120);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.activity_live_play);

        mUserId = UserInfoUtil.getMiTencentId();
        mNickname = UserInfoUtil.getName();
        mAvatar = UserInfoUtil.getAvatar();

        mList = (ArrayList<LivingListResponse.LiveListBean>) getIntent().getSerializableExtra("LIST");
        currentPosition = getIntent().getIntExtra("POSITION", 0);

        vod_type = mList.get(currentPosition).getVod_type();
        mLiveID = String.valueOf(mList.get(currentPosition).getLive_id());

        mFURenderer = new FURenderer
                .Builder(mContext)
                .createEGLContext(false)
                .maxFaces(2)
                .setNeedFaceBeauty(true)
                .inputPropOrientation(0)
                .setOnTrackingStatusChangedListener(this)
                .build();

        initView();

        mPlayerList = new TCVideoWidgetList(mRootContainer, null);
        Foreground.get().setListen(this);

        startLivePlay();
    }

    private JoinLiveResponse mJoinLiveResponse;
    protected String mPusherId;//房间创建者ID
    private int mCurrentMemberCount = 0;//观众人数
    private int mWatchNum = 0;

    private int vod_type = 1;

    private void initData(){
        mAvatarListAdapter = new TCUserAvatarListAdapter(this, mPusherId, mUserId, mLiveID);
        mUserAvatarList.setAdapter(mAvatarListAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mUserAvatarList.setLayoutManager(linearLayoutManager);

        mBroadcastingName.setText(TCUtils.getLimitString(mJoinLiveResponse.getLive_user().getUser_nickname(), 10));
        mHostID.setText("ID:" + mPusherId);
        Glide.with(mContext)
                .load(CommonUtils.getUrl(mJoinLiveResponse.getLive_user().getAvatar()))
                .into(mHeadIcon);
        mHeadIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LiveUserDialogFragment fragment = new LiveUserDialogFragment();
                Bundle bundle = new Bundle();
                bundle.putString("user_id", mPusherId);
                bundle.putString("meUserId", mUserId);
                bundle.putString("liveUid", mPusherId);
                bundle.putString("liveID", mLiveID);
                fragment.setArguments(bundle);
                fragment.show(getSupportFragmentManager(), "LiveUserDialogFragment");
            }
        });

        mMemberCount.setText(String.format(Locale.CHINA,"%d" , mCurrentMemberCount));

        mLiveRoomViewHolder.setVotes(String.valueOf(mJoinLiveResponse.getLive_user().getTotal_coin_num()));
        mLiveRoomViewHolder.setGuardNum(mWatchNum);

        if (TextUtils.isEmpty(mJoinLiveResponse.getIs_patrol()) || mJoinLiveResponse.getIs_patrol().equals("0")){
            mBtnPatrol.setVisibility(View.GONE);
        }else {
            mBtnPatrol.setVisibility(View.VISIBLE);
            mBtnPatrol.setOnClickListener(this);
        }
        if(mJoinLiveResponse.getIs_follow() == 1){
            mFollowTv.setVisibility(View.GONE);
        }else {
            mFollowTv.setVisibility(View.VISIBLE);
        }
        mFollowTv.setOnClickListener(this);

        TCSimpleUserInfo userInfo = new TCSimpleUserInfo(mUserId, mNickname, mAvatar);
        mAvatarListAdapter.addItem(userInfo);

        //系统通知
        TCChatEntity entity_ = new TCChatEntity(TCConstants.IMCMD_SYSTEM);
        entity_.setContent(mJoinLiveResponse.getLive_warning().getContent());
        notifyMsg(entity_);
    }

    protected void initView() {
        mViewPager = findViewById(R.id.view_pager);
        mViewPager.setAdapter(new PagerAdapter());

        mRootContainer = (ViewGroup) LayoutInflater.from(mContext).inflate(R.layout.live_play_item, null);

        rlPlayRoot = mRootContainer.findViewById(R.id.rl_play_root);
        rlPlayRoot.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return mTCSwipeAnimationController.processEvent(event, TCLivePlayerActivity.this);
            }
        });

        RelativeLayout controllLayer = mRootContainer.findViewById(R.id.rl_controllLayer);
        mTCSwipeAnimationController = new TCSwipeAnimationController(this);
        mTCSwipeAnimationController.setAnimationView(controllLayer);

        mTXCloudVideoView = mRootContainer.findViewById(R.id.video_view);

        mListViewMsg = mRootContainer.findViewById(R.id.im_msg_listview);
        mListViewMsg.setOnItemClickListener(this);
        mHeartLayout = mRootContainer.findViewById(R.id.heart_layout);
        mUserAvatarList = mRootContainer.findViewById(R.id.rv_user_avatar);

        mInputTextMsgDialog = new TCInputTextMsgDialog(this, R.style.InputDialog);
        mInputTextMsgDialog.setmOnTextSendListener(this);

        mHeadIcon = mRootContainer.findViewById(R.id.iv_head_icon);
        mBroadcastingName = mRootContainer.findViewById(R.id.tv_broadcasting_name);
        mHostID = mRootContainer.findViewById(R.id.tv_host_id);

        mMemberCount = mRootContainer.findViewById(R.id.tv_member_counts);

        mChatMsgListAdapter = new TCChatMsgListAdapter(this, mListViewMsg, mArrayListChatEntity, mRootContainer.findViewById(R.id.im_msg_new));
        mListViewMsg.setAdapter(mChatMsgListAdapter);

        if (mLiveDanmuPresenter == null) {
            mLiveDanmuPresenter = new LiveDanmuPresenter(mContext, (ViewGroup)mRootContainer.findViewById(R.id.danmakuView));
        }
        if (mLivePromptPresenter == null) {
            mLivePromptPresenter = new LivePromptPresenter(mContext, (ViewGroup)mRootContainer.findViewById(R.id.promptView));
        }
        mBgImageView = mRootContainer.findViewById(R.id.background);

        mBtnLinkMic = mRootContainer.findViewById(R.id.btn_linkmic);
        mBtnLinkMic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mIsBeingLinkMic == false) {
                    long curTime = System.currentTimeMillis();
                    if (curTime < mLastLinkMicTime + LINKMIC_INTERVAL) {
                        //NToast.shortToast(mContext, "太频繁啦，休息一下！");
                    } else {
                        mLastLinkMicTime = curTime;
                        startLinkMic();
                    }
                } else {
                    DialogUitl.showSimpleHintDialog(mContext, getString(R.string.prompt), getString(R.string.confirm), getString(R.string.cancel),
                            "退出连麦", true, true,
                            new DialogUitl.SimpleCallback2() {
                                @Override
                                public void onCancelClick() {

                                }
                                @Override
                                public void onConfirmClick(Dialog dialog, String content) {
                                    dialog.dismiss();

                                    stopLinkMic();
                                    startPlay();
                                }
                            });
                }
            }
        });

        mBeautyHepler = new TCBeautyHelperNew(mFURenderer);
        mBtnBeauty = mRootContainer.findViewById(R.id.btn_beauty);

        mLiveRoomViewHolder = new LiveRoomViewHolder(mContext, (ViewGroup)mRootContainer.findViewById(R.id.container), (GifImageView)mRootContainer.findViewById(R.id.gift_gif), (SVGAImageView)mRootContainer.findViewById(R.id.gift_svga));
        mLiveRoomViewHolder.addToParent();

        mRootContainer.findViewById(R.id.btn_message_input).setOnClickListener(this);
        mBtnSwitchCamera = mRootContainer.findViewById(R.id.btn_switch_cam);
        mBtnSwitchCamera.setOnClickListener(this);
        mBtnBeauty.setOnClickListener(this);
        mRootContainer.findViewById(R.id.btn_gift).setOnClickListener(this);
        mRootContainer.findViewById(R.id.btn_play_msg).setOnClickListener(this);
        mRootContainer.findViewById(R.id.btn_shop).setOnClickListener(this);
        mRootContainer.findViewById(R.id.btn_rotary).setOnClickListener(this);
        mRootContainer.findViewById(R.id.btn_share).setOnClickListener(this);
        mRootContainer.findViewById(R.id.btn_back).setOnClickListener(this);
        mRootContainer.findViewById(R.id.btn_redpacket).setOnClickListener(this);
        mRootContainer.findViewById(R.id.btn_live_rp).setOnClickListener(this);
        mBtnPatrol = mRootContainer.findViewById(R.id.btn_patrol);
        mFollowTv = mRootContainer.findViewById(R.id.layout_live_pusher_info_follow);

        //pk
        ViewGroup mContainer3 = mRootContainer.findViewById(R.id.container3);
        int widthPixels = mWidthPixels / 2;
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(mWidthPixels, (int)(widthPixels * Const.ratio));
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        layoutParams.topMargin = mTopMargin;
        mContainer3.setLayoutParams(layoutParams);
        mLiveLinkMicPkPresenter = new LiveLinkMicPkPresenter(mContext, mContainer3, false);

        mLoadView = findViewById(R.id.activity_view_loading);
        mLoadView.show();
        mLoadView.getBack().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exit();
            }
        });
    }

    @Override
    public void onAnchorEnter(AnchorInfo pusherInfo) {
        if (!mIsBeingLinkMic){
            return;
        }
        onAnchorEnter_(pusherInfo);
    }

    private void onAnchorEnter_(final AnchorInfo pusherInfo){
        if (pusherInfo == null || pusherInfo.userID == null) {
            return;
        }

        final TCVideoWidget videoView = mPlayerList.applyVideoView(pusherInfo.userID);
        if (videoView == null) {
            return;
        }

        if (mPusherList != null) {
            boolean exist = false;
            for (AnchorInfo item: mPusherList) {
                if (pusherInfo.userID.equalsIgnoreCase(item.userID)) {
                    exist = true;
                    break;
                }
            }
            if (exist == false) {
                mPusherList.add(pusherInfo);
            }
        }

        videoView.startLoading();
        mLiveRoom.startRemoteView(pusherInfo, videoView.videoView, new IMLVBLiveRoomListener.PlayCallback() {
            @Override
            public void onBegin() {
                videoView.stopLoading(false); //推流成功，stopLoading 小主播隐藏踢人的button
            }

            @Override
            public void onError(int errCode, String errInfo) {
                videoView.stopLoading(false);
                onDoAnchorExit(pusherInfo);
            }

            @Override
            public void onEvent(int event, Bundle param) {
                report(event);
            }
        }); //开启远端视频渲染
    }

    @Override
    public void onAnchorExit(AnchorInfo pusherInfo) {
        if (pusherInfo.userID.equals(mUserId)){
            stopLinkMic();
        }else {
            onDoAnchorExit(pusherInfo);
        }
    }

    private void onDoAnchorExit(AnchorInfo pusherInfo) {
        if (mPusherList != null) {
            Iterator<AnchorInfo> it = mPusherList.iterator();
            while (it.hasNext()) {
                AnchorInfo item = it.next();
                if (pusherInfo.userID.equalsIgnoreCase(item.userID)) {
                    it.remove();
                    break;
                }
            }
        }

        mLiveRoom.stopRemoteView(pusherInfo);//关闭远端视频渲染
        mPlayerList.recycleVideoView(pusherInfo.userID);
    }

    @Override
    public void onLivePrompt(LivePromptBean bean) {
        if (mLivePromptPresenter != null) {
            mLivePromptPresenter.showDanmu(bean);
        }
    }

    @Override
    public void onRequestJoinAnchor(AnchorInfo anchorInfo) {
    }

    @Override
    public void onRequestRoomAA(AnchorInfo anchorInfo) {
    }

    @Override
    public void onRequestRoomPK(AnchorInfo anchorInfo) {
    }

    @Override
    public void onPKLiveMsg(String type, AnchorInfo anchorInfo, String pkRestTime) {
        rlPlayRoot.setBackgroundResource(R.mipmap.bg_live_pk);
        if (mLiveLinkMicPkPresenter != null){
            int pkTime = 0;
            if (!TextUtils.isEmpty(pkRestTime)){
                pkTime = Integer.parseInt(pkRestTime);
            }
            //TODO
            //TODO
            //TODO
            //TODO
            mLiveLinkMicPkPresenter.onEnterRoomPkStart(0, 0, pkTime, 0, mJoinLiveResponse.getLive_user().getUser_nickname(), anchorInfo.userName, mPusherId, anchorInfo.userID);
        }
    }

    @Override
    public void onPKLiveFinish(String self_get_coin, String other_get_coin, String pk_punish_rest_time) {
        long leftGift = 0;
        long rightGift = 0;
        int pkRestTime = 0;
        if (!TextUtils.isEmpty(self_get_coin)){
            leftGift = Long.parseLong(self_get_coin);
        }
        if (!TextUtils.isEmpty(other_get_coin)){
            rightGift = Long.parseLong(other_get_coin);
        }
        if (!TextUtils.isEmpty(pk_punish_rest_time)){
            pkRestTime = Integer.parseInt(pk_punish_rest_time);
        }
        if (mLiveLinkMicPkPresenter != null){
            mLiveLinkMicPkPresenter.inPunishment(leftGift, rightGift, pkRestTime);
        }
    }

    @Override
    public void updateGift(String self_get_coin, String other_get_coin) {
        long leftGift = 0;
        long rightGift = 0;
        if (!TextUtils.isEmpty(self_get_coin)){
            leftGift = Long.parseLong(self_get_coin);
        }
        if (!TextUtils.isEmpty(other_get_coin)){
            rightGift = Long.parseLong(other_get_coin);
        }
        if (mLiveLinkMicPkPresenter != null){
            mLiveLinkMicPkPresenter.onEnterRoomPkStart(leftGift, rightGift);
        }
    }

    @Override
    public void liveClosePK() {
        if (mLiveLinkMicPkPresenter != null){
            mLiveLinkMicPkPresenter.onLinkMicPkClose_();
        }
    }

    @Override
    public void onQuitRoomAA() {
    }

    @Override
    public void likeMsg(MegUserInfo bean){
        if (mHeartLayout != null) {
            mHeartLayout.addFavor();
        }

        AttributeInfo attributeInfo = new AttributeInfo();
        attributeInfo.setIdentityType(bean.getIdentity_type());
        attributeInfo.setIsGuard(bean.getIs_watch());
        attributeInfo.setIsVip(bean.getIs_vip());
        attributeInfo.setUser_level(bean.getLevel());
        attributeInfo.setMount("");
        TCChatEntity entity = new TCChatEntity(bean.getNickname(), attributeInfo, TCConstants.IMCMD_PRAISE, bean.getUser_id());
        entity.setContent(getString(R.string.like));
        notifyMsg(entity);
    }

    @Override
    public void guardianMsg(MegUserInfo bean, String total_watch_num){
        AttributeInfo attributeInfo = new AttributeInfo();
        attributeInfo.setIdentityType(bean.getIdentity_type());
        attributeInfo.setIsGuard(bean.getIs_watch());
        attributeInfo.setIsVip(bean.getIs_vip());
        attributeInfo.setUser_level(bean.getLevel());
        attributeInfo.setMount("");
        TCChatEntity entity = new TCChatEntity(bean.getNickname(), attributeInfo, TCConstants.IMCMD_PAILN_TEXT, bean.getUser_id());
        entity.setContent(getString(R.string.guard_host));
        notifyMsg(entity);

        if (!TextUtils.isEmpty(total_watch_num)){
            mWatchNum = Integer.parseInt(total_watch_num);
            if (mLiveRoomViewHolder != null){
                mLiveRoomViewHolder.setGuardNum(mWatchNum);
            }
        }
    }

    @Override
    public void onKickoutPeople(String user_id) {
        if (mUserId.equals(user_id)){
            showErrorAndQuit(TCConstants.ERROR_2);
        }
    }

    @Override
    public void liveClose(){
        showErrorAndQuit(TCConstants.ERROR_3);
    }

    @Override
    public void allCoinChange(String coin){
        if (mLiveRoomViewHolder != null){
            mLiveRoomViewHolder.setVotes(coin);
        }
    }

    @Override
    public void onTxtMsg(boolean danmu, MegUserInfo bean, String content){
        AttributeInfo attributeInfo = new AttributeInfo();
        attributeInfo.setIdentityType(bean.getIdentity_type());
        attributeInfo.setIsGuard(bean.getIs_watch());
        attributeInfo.setIsVip(bean.getIs_vip());
        attributeInfo.setUser_level(bean.getLevel());
        attributeInfo.setMount("");
        TCChatEntity entity = new TCChatEntity(bean.getNickname(), attributeInfo, TCConstants.IMCMD_PAILN_TEXT, bean.getUser_id());
        entity.setContent(content);
        notifyMsg(entity);

        if (danmu && mLiveDanmuPresenter != null) {
            mLiveDanmuPresenter.showDanmu(new DanmuBean(bean.getUser_id(), bean.getNickname(), bean.getAvatar(), content));
        }
    }

    @Override
    public void onEnterLive(MegUserInfo bean) {
        TCSimpleUserInfo userInfo = new TCSimpleUserInfo(bean.getUser_id(), bean.getNickname(), bean.getAvatar());
        if (mAvatarListAdapter != null){
            mAvatarListAdapter.addItem(userInfo);
        }
        if (!bean.getUser_id().equals(mUserId)){
            mCurrentMemberCount++;
        }
        mMemberCount.setText(String.format(Locale.CHINA,"%d",mCurrentMemberCount));
        AttributeInfo attributeInfo = new AttributeInfo();
        attributeInfo.setIdentityType(bean.getIdentity_type());
        attributeInfo.setIsGuard(bean.getIs_watch());
        attributeInfo.setIsVip(bean.getIs_vip());
        attributeInfo.setUser_level(bean.getLevel());
        attributeInfo.setMount(bean.getMount());
//        TCChatEntity entity = new TCChatEntity(userInfo.nickname, attributeInfo, TCConstants.IMCMD_ENTER_LIVE, userInfo.userid);
//        entity.setContent("加入直播");
//        notifyMsg(entity);
        if (mLiveRoomViewHolder != null){
            mLiveRoomViewHolder.onEnterRoom(userInfo, attributeInfo);
        }
    }

    @Override
    public void onExitLive(MegUserInfo bean) {
        if(mCurrentMemberCount > 0){
            mCurrentMemberCount--;
        } else{
            NLog.e(TAG, "接受多次退出请求，目前人数为负数");
        }
        mMemberCount.setText(String.format(Locale.CHINA,"%d",mCurrentMemberCount));
        mAvatarListAdapter.removeItem(bean.getUser_id());
//        AttributeInfo attributeInfo = new AttributeInfo();
//        attributeInfo.setIdentityType(bean.getIdentity_type());
//        attributeInfo.setIsGuard(bean.getIs_watch());
//        attributeInfo.setIsVip(bean.getIs_vip());
//        attributeInfo.setUser_level(bean.getLevel());
//        attributeInfo.setMount("");
//        TCChatEntity entity = new TCChatEntity(bean.getNickname(), attributeInfo, TCConstants.IMCMD_EXIT_LIVE, bean.getUser_id());
//        entity.setContent("退出直播");
//        notifyMsg(entity);
    }

    @Override
    public void onGiftMsg(MegUserInfo beanMeg, MegGift beanGift) {
        if (mLiveRoomViewHolder != null){
            LiveReceiveGiftBeanItem item = new LiveReceiveGiftBeanItem();
            item.setGiftId(beanGift.getGift_uni_code());
            item.setGiftName(beanGift.getName());
            item.setGiftIcon(CommonUtils.getUrl(beanGift.getIcon_img()));
            item.setGifUrl(CommonUtils.getUrl(beanGift.getEffect_img()));
            item.setStyle(Integer.parseInt(beanGift.getStyle()));
            item.setGiftCount(Integer.parseInt(beanGift.getNumber()));
            item.setVotes("0");
            LiveReceiveGiftBean bean = new LiveReceiveGiftBean();
            bean.setLiveReceiveGiftBeanItem(item);
            bean.setGifPath("");
            bean.setUid(beanMeg.getUser_id());
            bean.setAvatar(beanMeg.getAvatar());
            bean.setUserNiceName(beanMeg.getNickname());
            mLiveRoomViewHolder.showGiftMessage(bean);
            AttributeInfo attributeInfo = new AttributeInfo();
            attributeInfo.setIdentityType(beanMeg.getIdentity_type());
            attributeInfo.setIsGuard(beanMeg.getIs_watch());
            attributeInfo.setIsVip(beanMeg.getIs_vip());
            attributeInfo.setUser_level(beanMeg.getLevel());
            attributeInfo.setMount("");
            //聊天消息中插入 送礼物 提示
            TCChatEntity entity = new TCChatEntity(beanMeg.getNickname(), attributeInfo, TCConstants.IMCMD_GIFT, beanMeg.getUser_id());
            entity.setContent(getString(R.string.live_send_gift_1) + bean.getLiveReceiveGiftBeanItem().getGiftCount() + "个" + bean.getLiveReceiveGiftBeanItem().getGiftName());
            notifyMsg(entity);
        }
    }

    @Override
    public void onError(int errorCode, String errorMessage, Bundle extraInfo) {
        if (errorCode == MLVBCommonDef.LiveRoomErrorCode.ERROR_IM_FORCE_OFFLINE) { // IM 被强制下线。
            Intent intent = new Intent();
            intent.setAction(TCConstants.EXIT_APP);
            LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
        } else {
            showErrorAndQuit(errorCode);
        }
    }

    /**
     * @param event
     */
    private void report(int event) {
        switch (event) {
            case TXLiveConstants.PLAY_EVT_RCV_FIRST_I_FRAME :
                //视频播放成功
                break;
            case TXLiveConstants.PLAY_ERR_NET_DISCONNECT :
                //网络断连,且经多次重连抢救无效,可以放弃治疗,更多重试请自行重启播放
                break;
            case TXLiveConstants.PLAY_ERR_GET_RTMP_ACC_URL_FAIL :
                //获取加速拉流地址失败
                break;
            case TXLiveConstants.PLAY_ERR_FILE_NOT_FOUND :
                //播放文件不存在
                break;
            case TXLiveConstants.PLAY_ERR_HEVC_DECODE_FAIL :
                //H265解码失败
                break;
            case TXLiveConstants.PLAY_ERR_HLS_KEY :
                //HLS解码Key获取失败
                break;
            case TXLiveConstants.PLAY_ERR_GET_PLAYINFO_FAIL :
                //获取点播文件信息失败
                break;

        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (mArrayListChatEntity == null || mArrayListChatEntity.size() < 0){
            return;
        }
        if (mArrayListChatEntity.get(position).getType() == TCConstants.IMCMD_SYSTEM){
            return;
        }
        LiveUserDialogFragment fragment = new LiveUserDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("user_id", mArrayListChatEntity.get(position).getUser_id());
        bundle.putString("meUserId", mUserId);
        bundle.putString("liveUid", mPusherId);
        bundle.putString("liveID", mLiveID);
        fragment.setArguments(bundle);
        fragment.show(getSupportFragmentManager(), "LiveUserDialogFragment");
    }

    public void userDialog(String user_id){
        LiveUserDialogFragment fragment = new LiveUserDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("user_id", user_id);
        bundle.putString("meUserId", mUserId);
        bundle.putString("liveUid", mPusherId);
        bundle.putString("liveID", mLiveID);
        fragment.setArguments(bundle);
        fragment.show(getSupportFragmentManager(), "LiveUserDialogFragment");
    }

    @Override
    public void onItemClick(Integer bean, int position) {
        if (isHeart){
            if (mHeartLayout != null) {
                mHeartLayout.addFavor();
            }
        }else {
            isHeart = true;
            //点赞发送请求限制
            if (mLikeFrequeControl == null) {
                mLikeFrequeControl = new TCFrequeControl();
                mLikeFrequeControl.init(2, 1);
            }
//            TCSimpleUserInfo userInfo = new TCSimpleUserInfo(mUserId, mNickname, mHeadPic);
//            handlePraiseMsg(userInfo, mAttributeInfo);
            if (mLikeFrequeControl.canTrigger()) {
                //点赞消息
                mLiveRoom.sendRoomMsg(TCConstants.IMCMD_PRAISE, "",null);
            }
        }
    }

    protected void startPlay() {
        if (mPlaying) return;
        mLiveRoom.setListener(this);
        mLiveRoom.enterRoom(vod_type, mLiveID, mJoinLiveResponse.getB_live_url().getHttp_flv(), mTXCloudVideoView, new IMLVBLiveRoomListener.EnterRoomCallback() {
            @Override
            public void onError(int errCode, String errInfo) {
                showErrorAndQuit(TCConstants.ERROR_4);
                //进入LiveRoom失败
            }

            @Override
            public void onSuccess() {
                mLoadView.hide();

                //进入LiveRoom成功
//                if (!pureAudio){
//                    mBgImageView.setVisibility(View.GONE);
//                }

                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        String body_ = "";
                        try {
                            body_ = new JsonBuilder()
                                    .put("scene_id", 2)//场景（1：直播列表页顶部 2：直播房间页顶部）
                                    .build();
                        } catch (JSONException e) {
                        }
                        OKHttpUtils.getInstance().getRequest("app/ad/getNotice", body_, new com.ningmengs789.live.Interface.RequestCallback() {
                            @Override
                            public void onError(int errCode, String errInfo) {
                                mLiveRoomViewHolder.getLiveNoticeFl().setVisibility(View.GONE);
                            }
                            @Override
                            public void onSuccess(String result) {
                                if (result != null) {
                                    try {
                                        NoticeResponse response = JsonMananger.jsonToBean(result, NoticeResponse.class);
                                        String text = response.getContent().getText();
                                        if (TextUtils.isEmpty(text)){
                                            return;
                                        }
                                        mLiveRoomViewHolder.getLiveNoticeFl().setVisibility(View.VISIBLE);
                                        mLiveRoomViewHolder.getLiveNoticeContent().setText(text);

                                        final String link = response.getContent().getLink();
                                        mLiveRoomViewHolder.getLiveNoticeContent().setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                if (!TextUtils.isEmpty(link)) {
                                                    Intent intent = new Intent("io.xchat.intent.action.webview");
                                                    intent.setPackage(mContext.getPackageName());
                                                    intent.putExtra("url", link);
                                                    startActivity(intent);
                                                }
                                            }
                                        });
                                    } catch (HttpException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        });

                        mLiveRoom.sendRoomMsg(TCConstants.IMCMD_ENTER_LIVE, "", new RequestCallback() {
                            @Override
                            public void onError(int errCode, String errInfo) {
                            }

                            @Override
                            public void onSuccess(String result) {
                                try {
                                    JSONObject data = new JSONObject(result);
                                    NLog.e(TAG, result);
                                    String is_pk = data.getString("is_pk");
                                    if (is_pk.equals("1")){
                                        rlPlayRoot.setBackgroundResource(R.mipmap.bg_live_pk);
                                        long self_get_coin = data.getJSONObject("pk_info").getLong("self_get_coin");
                                        long other_get_coin = data.getJSONObject("pk_info").getLong("other_get_coin");
                                        int pk_rest_time = data.getJSONObject("pk_info").getInt("pk_rest_time");
                                        int pk_punish_rest_time = data.getJSONObject("pk_info").getInt("pk_punish_rest_time");
                                        int pkTime = 0;
                                        int isPkEnd = 0;
                                        //isPkEnd 0PK状态 1惩罚状态
                                        if (pk_rest_time > 0){
                                            pkTime = pk_rest_time;
                                            isPkEnd = 0;
                                        }else {
                                            pkTime = pk_punish_rest_time;
                                            isPkEnd = 1;
                                        }
                                        if (mLiveLinkMicPkPresenter != null){
                                            //TODO
                                            //TODO
                                            //TODO
                                            //TODO
                                            String user_nickname = data.getJSONObject("other_user_info").getString("user_nickname");
                                            String user_id = data.getJSONObject("other_user_info").getString("user_id");
                                            mLiveLinkMicPkPresenter.onEnterRoomPkStart(self_get_coin, other_get_coin, pkTime, isPkEnd, mJoinLiveResponse.getLive_user().getUser_nickname(), user_nickname, mPusherId, user_id);
                                        }
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        });

                        String body = "";
                        try {
                            body = new JsonBuilder()
                                    .put("live_id", mLiveID)
                                    .build();
                        } catch (JSONException e) {
                        }
                        OKHttpUtils.getInstance().getRequest("app/live/getLiveViewer", body, new com.ningmengs789.live.Interface.RequestCallback() {
                            @Override
                            public void onError(int errCode, String errInfo) {
                                NLog.e(TAG, "获取观众列表失败：" + errInfo);
                            }
                            @Override
                            public void onSuccess(String result) {
                                try{
                                    LiveViewerResponse liveViewerResponse = JsonMananger.jsonToBean(result, LiveViewerResponse.class);
                                    int size = liveViewerResponse.getList().size();
                                    if (mCurrentMemberCount < size){
                                        mCurrentMemberCount = size - 1;
                                        mMemberCount.setText(String.format(Locale.CHINA,"%d",mCurrentMemberCount));
                                    }

                                    for (int i = 0; i < size; i++) {
                                        TCSimpleUserInfo userInfo = new TCSimpleUserInfo(
                                                String.valueOf(liveViewerResponse.getList().get(i).getUser_id()),
                                                liveViewerResponse.getList().get(i).getUser_nickname(),
                                                CommonUtils.getUrl(liveViewerResponse.getList().get(i).getAvatar()));
                                        mAvatarListAdapter.addItem(userInfo);
                                    }
                                }catch (Exception e){
                                }
                            }
                        });

                    }
                }, 1000);
            }
        });
        mPlaying = true;
    }

    private void stopPlay() {
//        LoadDialog.show(this);
        if (mPlaying && mLiveRoom != null) {
            mLiveRoom.exitRoom(new IMLVBLiveRoomListener.ExitRoomCallback() {
                @Override
                public void onError(int errCode, String errInfo) {
//                    NLog.e(TAG, "exit room error : "+errInfo);
//                    LoadDialog.dismiss(TCLivePlayerActivity.this);
                    stopLinkMic();
                    mLiveID = null;
                    exit();
                }

                @Override
                public void onSuccess() {
//                    NLog.e(TAG, "exit room success ");
//                    LoadDialog.dismiss(TCLivePlayerActivity.this);
                    stopLinkMic();
                    mLiveID = null;
                    exit();
                }
            });
            mPlaying = false;
            mLiveRoom.setListener(null);
        }
    }

    private void exit(){
        SocketUtil.getInstance().cancelSocketMsgListener();
        finish();

        if (isSliding){
            Intent intent = new Intent(mContext, TCLivePlayerActivity.class);
            intent.putExtra("LIST", mList);
            intent.putExtra("POSITION", currentPosition);
            startActivity(intent);
            overridePendingTransition(R.anim.anim_null, R.anim.anim_null);
        }
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.anim_null, R.anim.anim_null);
    }

    /**
     * 发消息弹出框
     */
    private void showInputMsgDialog() {
        WindowManager windowManager = getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = mInputTextMsgDialog.getWindow().getAttributes();

        lp.width = (display.getWidth()); //设置宽度
        mInputTextMsgDialog.getWindow().setAttributes(lp);
        mInputTextMsgDialog.setCancelable(true);
        mInputTextMsgDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        mInputTextMsgDialog.show();
    }

    private void showErrorAndQuit(int errorCode) {
        //ERROR_1 = 1001;//观众拉流失败
        //ERROR_2 = 1002;//观众被T出直播间
        //ERROR_3 = 1003;//观众收到 主播关闭直播间
        //ERROR_4 = 1004;//观众 进入直播间失败
        switch (errorCode){
            case TCConstants.ERROR_1:
//                NToast.shortToast(mContext, "网络异常，请检查设置");
                break;
            case TCConstants.ERROR_3:
                //图片提示
                mBgImageView.setVisibility(View.VISIBLE);
                stopLinkMic();
                break;
            case TCConstants.ERROR_2:
                NToast.shortToast(mContext, "不好意思，您被管理踢出直播间");
                showComfirmDialog();
                break;
            case TCConstants.ERROR_4:
                NToast.shortToast(mContext, "进入直播间失败");
                showComfirmDialog();
                break;
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_back:
                showComfirmDialog();
                break;
            case R.id.btn_message_input:
                showInputMsgDialog();
                break;
            case R.id.btn_share:
                showShareDialog();
                break;
            case R.id.btn_gift:
                openGiftWindow();
                break;
            case R.id.btn_rotary:
                RotaryTableFragment fragmentRotary = new RotaryTableFragment();
                fragmentRotary.show(getSupportFragmentManager(), "RotaryTableFragment");
                break;
            case R.id.btn_shop:
                String url = Const.getDomain() + "/apph5/shop/goodslist";
                ShopDialogFragment fragment = new ShopDialogFragment();
                Bundle bundle = new Bundle();
                bundle.putString("url", url);
                bundle.putString("USERID", mPusherId);
                fragment.setArguments(bundle);
                fragment.show(getSupportFragmentManager(), "ShopDialogFragment");
                break;
            case R.id.btn_beauty:
                if (mBeautyHepler.isAddedMain()) {
                    mBeautyHepler.dismissMain();
                }else {
                    mBeautyHepler.showMain(getFragmentManager(), "BeautyMainFragment");
                }
                break;
            case R.id.btn_switch_cam:
                if (mIsBeingLinkMic) {
                    mLiveRoom.switchCamera();
                }
                break;
            case R.id.btn_play_msg:
                openChatRoomWindow();
                break;
            case R.id.layout_live_pusher_info_follow:
                String body = "";
                try {
                    body = new JsonBuilder()
                            .put("be_user_id", mPusherId)
                            .build();
                } catch (JSONException e) {
                }
                OKHttpUtils.getInstance().getRequest("app/follow/add", body, new com.ningmengs789.live.Interface.RequestCallback() {
                    @Override
                    public void onError(int errCode, String errInfo) {
                        NToast.shortToast(mContext, errInfo);
                    }
                    @Override
                    public void onSuccess(String result) {
                        mFollowTv.setVisibility(View.GONE);
                    }
                });
                break;
            case R.id.btn_patrol:
                SuperManagerDialogFragment smFragment = new SuperManagerDialogFragment();
                Bundle bundleSM = new Bundle();
                bundleSM.putString("mLiveId", mLiveID);
                smFragment.setArguments(bundleSM);
                smFragment.show(getSupportFragmentManager(), "SuperManagerDialogFragment");
                break;
            case R.id.btn_redpacket:
                SendRedPacketDialogFragment srpdf = new SendRedPacketDialogFragment();
                Bundle srpBundle = new Bundle();
                srpBundle.putString("mLiveId", mLiveID);
                srpBundle.putString("zbUserId", mPusherId);
                srpdf.setArguments(srpBundle);
                srpdf.show(getSupportFragmentManager(), "SendRedPacketDialogFragment");
                break;
            case R.id.btn_live_rp:
                RobRedPacketDialogFragment rrpdf = new RobRedPacketDialogFragment();
                Bundle rrpBundle = new Bundle();
                rrpBundle.putString("mLiveId", mLiveID);
                rrpBundle.putString("zbUserId", mPusherId);
                rrpdf.setArguments(rrpBundle);
                rrpdf.show(getSupportFragmentManager(), "RobRedPacketDialogFragment");
                break;
            default:
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mLiveDanmuPresenter != null) {
            mLiveDanmuPresenter.release();
            mLiveDanmuPresenter.reset();
        }

        if (mLivePromptPresenter != null) {
            mLivePromptPresenter.release();
            mLivePromptPresenter.reset();
        }

        if (mLiveRoomViewHolder != null) {
            mLiveRoomViewHolder.clearData();
        }

        if (mLiveLinkMicPkPresenter != null){
            mLiveLinkMicPkPresenter.release();
        }

        mPlayerList.recycleVideoView();
        mPlayerList = null;

        Foreground.get().setListen(null);
    }

    private void notifyMsg(final TCChatEntity entity) {
        if (mArrayListChatEntity.size() > 1000) {
            while (mArrayListChatEntity.size() > 900) {
                mArrayListChatEntity.remove(0);
            }
        }

        mArrayListChatEntity.add(entity);
        mChatMsgListAdapter.notifyDataSetChanged();
    }

    /**
     * TextInputDialog发送回调
     * @param msg 文本信息
     * @param danmuOpen 是否打开弹幕
     */
    @Override
    public void onTextSend(String msg, boolean danmuOpen) {
        if (msg.length() == 0)
            return;
        try {
            byte[] byte_num = msg.getBytes("utf8");
            if (byte_num.length > 160) {
                NToast.shortToast(mContext, getString(R.string.sq_3));
                return;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return;
        }

        if (danmuOpen) {
            mLiveRoom.sendRoomMsg(TCConstants.IMCMD_DANMU, msg, null);
        } else {
            mLiveRoom.sendRoomMsg(TCConstants.IMCMD_PAILN_TEXT, msg, null);
        }
    }

    private void showShareDialog() {
        View view = getLayoutInflater().inflate(R.layout.share_dialog2, null);
        final AlertDialog mDialog = new AlertDialog.Builder(this, R.style.BottomDialog2).create();
        mDialog.show();

        Window window =mDialog.getWindow();
        if (window != null) {
            window.setContentView(view);
            window.setWindowAnimations(R.style.BottomDialog_Animation);
            WindowManager.LayoutParams mParams = window.getAttributes();
            mParams.width = android.view.WindowManager.LayoutParams.MATCH_PARENT;
            mParams.height = android.view.WindowManager.LayoutParams.WRAP_CONTENT;
            window.setGravity(Gravity.BOTTOM);
            window.setAttributes(mParams);
        }

        TextView btn_wx = view.findViewById(R.id.btn_share_wx);
        TextView btn_circle = view.findViewById(R.id.btn_share_circle);
        TextView btn_qq = view.findViewById(R.id.btn_share_qq);
        TextView btn_qzone = view.findViewById(R.id.btn_share_qzone);
        TextView btn_wb = view.findViewById(R.id.btn_share_wb);
        TextView btn_cancel = view.findViewById(R.id.btn_share_cancle);

        btn_wx.setOnClickListener(mShareBtnClickListen);
        btn_circle.setOnClickListener(mShareBtnClickListen);
        btn_qq.setOnClickListener(mShareBtnClickListen);
        btn_qzone.setOnClickListener(mShareBtnClickListen);
        btn_wb.setOnClickListener(mShareBtnClickListen);
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDialog.dismiss();
            }
        });
    }

    private View.OnClickListener mShareBtnClickListen = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_share_wx:
                    mShare_meidia = SHARE_MEDIA.WEIXIN;
                    break;
                case R.id.btn_share_circle:
                    mShare_meidia = SHARE_MEDIA.WEIXIN_CIRCLE;
                    break;
                case R.id.btn_share_qq:
                    mShare_meidia = SHARE_MEDIA.QQ;
                    break;
                case R.id.btn_share_qzone:
                    mShare_meidia = SHARE_MEDIA.QZONE;
                    break;
                case R.id.btn_share_wb:
                    mShare_meidia = SHARE_MEDIA.SINA;
                    break;
                default:
                    break;
            }

            String body = "";
            try {
                body = new JsonBuilder()
                        .put("id", mPusherId)
                        .put("vod_type", vod_type)
                        .build();
            } catch (JSONException e) {
            }
            OKHttpUtils.getInstance().getRequest("app/promotion/getLiveShare", body, new com.ningmengs789.live.Interface.RequestCallback() {
                @Override
                public void onError(int errCode, String errInfo) {
                    NToast.shortToast(mContext, errInfo);
                }
                @Override
                public void onSuccess(String result) {
                    if (result == null) {
                        return;
                    }
                    try {
                        PromDataResponse response = JsonMananger.jsonToBean(result, PromDataResponse.class);
                        String link = response.getShare_link().getLink();
                        String title = response.getShare_link().getTitle();
                        String desc = response.getShare_link().getDesc();
                        String icon_link = CommonUtils.getUrl(response.getShare_link().getIcon_url());
                        startShare(title, link, icon_link, desc);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    };

    private void startShare(String title, String link, String icon_link, String desc) {
        ShareAction shareAction = new ShareAction(this);
        String shareUrl = link;
        UMWeb web = new UMWeb(shareUrl);
        web.setThumb(new UMImage(this, icon_link));
        web.setTitle(title);
        web.setDescription(desc);
//        shareAction.withText(desc);
        shareAction.withMedia(web);
        shareAction.setCallback(umShareListener);
        shareAction.setPlatform(mShare_meidia).share();
    }

    private UMShareListener umShareListener = new UMShareListener() {

        @Override
        public void onStart(SHARE_MEDIA platform) {
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
//            NToast.shortToast(mContext, platform + " 分享成功啦");
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
//            NToast.shortToast(mContext, "分享失败");
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
//            NToast.shortToast(mContext, "分享取消了");
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /** attention to this below ,must add this**/
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
        NLog.d("result","onActivityResult");
    }

    private BeautyTieZhiFragment.BeautyParams beautyParamsTieZhi;
    private String proMeiFu;
    private String proMeiXing;
    private BeautyLvJingFragment.BeautyParams beautyParamsLvJing;

    private void joinPusher() {
        TCVideoWidget videoView = mPlayerList.getFirstRoomView();
        videoView.setUsed(true);
        videoView.userID = mUserId;

        mLiveRoom.startLocalPreview(true, videoView.videoView, mFURenderer);
        mLiveRoom.setCameraMuteImage(R.drawable.pause_publish);

        //###
        beautyParamsTieZhi = new BeautyTieZhiFragment.BeautyParams();
        String tiezhi_path = UserInfoUtil.getTiezhiPath();
        if (TextUtils.isEmpty(tiezhi_path)){
            beautyParamsTieZhi.mVideoMaterialMetaData = new VideoMaterialMetaData("video_none", "video_none", "", "");
        }else {
            beautyParamsTieZhi.mVideoMaterialMetaData = new VideoMaterialMetaData("video", tiezhi_path, "", "");
        }
        String miefu = UserInfoUtil.getMeifu();
        if (TextUtils.isEmpty(miefu)){
            proMeiFu = "0@@20@@20@@20@@20@@20";
        }else {
            proMeiFu = miefu;
        }
        String miexing = UserInfoUtil.getMeiXing();
        if (TextUtils.isEmpty(miexing)){
            proMeiXing = "0@@30@@30@@30@@30@@30@@30@@30@@30@@30";
        }else {
            proMeiXing = miexing;
        }
        int filterProgress = UserInfoUtil.getFilterProgress();
        String filterStr = UserInfoUtil.getFilterStr();
        beautyParamsLvJing = new BeautyLvJingFragment.BeautyParams();
        beautyParamsLvJing.mFilterProgress = filterProgress;
        if (TextUtils.isEmpty(filterStr)){
            beautyParamsLvJing.mFilter = Filter.Key.ORIGIN;
        }else {
            beautyParamsLvJing.mFilter = filterStr;
        }
        mBeautyHepler.init(beautyParamsTieZhi, proMeiFu, proMeiXing, beautyParamsLvJing);
        //###

        mLiveRoom.joinAnchor(mSelfPushUrl, new IMLVBLiveRoomListener.JoinAnchorCallback() {
            @Override
            public void onError(int errCode, String errInfo) {
                stopLinkMic();
                NToast.shortToast(mContext, errInfo);
            }

            @Override
            public void onSuccess() {
                mIsBeingLinkMic = true;

                String body = "";
                try {
                    body = new JsonBuilder()
                            .put("live_id", mLiveID)
                            .put("viewer_uid", mUserId)
                            .build();
                } catch (JSONException e) {
                }
                OKHttpUtils.getInstance().getRequest("app/live/viewerJoinLianmaiSuccess", body, new com.ningmengs789.live.Interface.RequestCallback() {
                    @Override
                    public void onError(int errCode, String errInfo) {
                        stopLinkMic();
                        NToast.shortToast(mContext, errInfo);
                    }
                    @Override
                    public void onSuccess(String result) {
                        try {
                            //获取房间里所有正在推流的成员
                            JSONObject data = new JSONObject(result);
                            JSONArray jsa = data.getJSONArray("viewer_b_live_quick_list");
                            updateAnchors(jsa);

                            mBtnLinkMic.setEnabled(true);
                            mIsBeingLinkMic = true;
                            if (mBtnSwitchCamera != null) {
                                mBtnSwitchCamera.setVisibility(View.VISIBLE);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
    }

    private void startLinkMic() {
        if (mIsBeingLinkMic) {
            return;
        }

        mBtnLinkMic.setEnabled(false);
        mBtnLinkMic.setImageResource(R.mipmap.icon_live_link_mic_1);

        String body = "";
        try {
            body = new JsonBuilder()
                    .put("live_id", mLiveID)
                    .build();
        } catch (JSONException e) {
        }
        OKHttpUtils.getInstance().getRequest("app/live/applyLianmai", body, new com.ningmengs789.live.Interface.RequestCallback() {
            @Override
            public void onError(int errCode, String errInfo) {
                mBtnLinkMic.setEnabled(true);
                NToast.shortToast(mContext, errInfo);
                mIsBeingLinkMic = false;
                mBtnLinkMic.setImageResource(R.mipmap.icon_live_link_mic);
                if (mBtnBeauty != null) {
                    mBtnBeauty.setVisibility(View.GONE);
                }
            }
            @Override
            public void onSuccess(String result) {
                NToast.shortToast(mContext, "等待主播接受......");
                isPostDelayed = false;
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (!isPostDelayed){
                            onXZBAccept("2", null, null);
                        }
                    }
                }, 10000);
            }
        });
    }
    private boolean isPostDelayed = false;

    private String mSelfPushUrl;
    @Override
    public void onXZBAccept(String type, String selfPushUrl, String accelerateURL){
        isPostDelayed = true;
        if (type.equals("1")){
            mSelfPushUrl = selfPushUrl;
            mLiveRoom.startPlayAccelerateURL(accelerateURL);

            NToast.shortToast(mContext, "主播接受了您的连麦请求，开始连麦");
            if (TCUtils.checkRecordPermission(TCLivePlayerActivity.this)) {
                joinPusher();
            }

            if (mBtnBeauty != null) {
                mBtnBeauty.setVisibility(View.VISIBLE);
            }
        }else {
            mBtnLinkMic.setEnabled(true);
            NToast.shortToast(mContext, "主播拒绝了你得连麦~");
            mIsBeingLinkMic = false;
            mBtnLinkMic.setImageResource(R.mipmap.icon_live_link_mic);
            if (mBtnBeauty != null) {
                mBtnBeauty.setVisibility(View.GONE);
            }
        }
    }

    private void updateAnchors(JSONArray jsa){
        if (jsa == null || jsa.length() == 0){
            return;
        }
        try {
            for (int i = 0; i < jsa.length(); i++) {
                JSONObject jso = jsa.getJSONObject(i);
                String userID = jso.getJSONObject("user_info").getString("user_id");
                if (!userID.equalsIgnoreCase(mUserId)){
                    String userName = jso.getJSONObject("user_info").getString("nickname");
                    String userAvatar = jso.getJSONObject("user_info").getString("avatar");
                    String rtmp = jso.getJSONObject("b_live_quick_url").getString("rtmp");
                    AnchorInfo anchorInfo = new AnchorInfo(userID, userName, userAvatar, rtmp);
                    onAnchorEnter_(anchorInfo);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private synchronized void stopLinkMic() {
        if (!mIsBeingLinkMic) return;

        mIsBeingLinkMic = false;

        //启用连麦Button
        if (mBtnLinkMic != null) {
            mBtnLinkMic.setEnabled(true);
            mBtnLinkMic.setImageResource(R.mipmap.icon_live_link_mic);
        }

        //隐藏切换摄像头Button
        if (mBtnSwitchCamera != null) {
            mBtnSwitchCamera.setVisibility(View.GONE);
        }

        if (mBtnBeauty != null) {
            mBtnBeauty.setVisibility(View.GONE);
        }

        mLiveRoom.stopLocalPreview();
        mLiveRoom.quitJoinAnchor(mUserId);

        if (mPlayerList != null) {
            mPlayerList.recycleVideoView(mUserId);
            mPusherList.clear();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 100:
                for (int ret : grantResults) {
                    if (ret != PackageManager.PERMISSION_GRANTED) {
                        return;
                    }
                }
                joinPusher();
                break;
            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        showComfirmDialog();
    }

    private void showComfirmDialog() {
        if (mIsBeingLinkMic){
            DialogUitl.showSimpleHintDialog(mContext, getString(R.string.prompt), "关闭连麦", "继续连麦",
                    "连麦中，关闭连麦？", true, true,
                    new DialogUitl.SimpleCallback2() {
                        @Override
                        public void onCancelClick() {

                        }
                        @Override
                        public void onConfirmClick(Dialog dialog, String content) {
                            dialog.dismiss();
                            stopLinkMic();
                        }
                    });
        }else {
            stopPlay();
        }
    }

    public void changeTXCVV(boolean isD){
        if (mTXCloudVideoView == null){
            return;
        }
        if (isD){
            rlPlayRoot.setBackgroundColor(Color.parseColor("#00000000"));
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
            mTXCloudVideoView.setLayoutParams(layoutParams);
        }else {
            int widthPixels = mWidthPixels / 2;
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(mWidthPixels, (int)(widthPixels * Const.ratio));
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            layoutParams.topMargin = mTopMargin;
//            layoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
            mTXCloudVideoView.setLayoutParams(layoutParams);
        }
    }

    //私信主播
    private void openChatRoomWindow(){
        String mi_tencentId = mUserId;
        String mi_platformId = mUserId;

        String prom_custom_uid = mPusherId;
        String user_id = mPusherId;

        if (mi_tencentId.equals(prom_custom_uid)){
            //自己不能跟自己聊天
            NToast.shortToast(mContext,  getString(R.string.not_me_chat));
            return;
        }

        if (prom_custom_uid.equals(user_id)){
            //真人
        }

        IMConversation imConversation = new IMConversation();
        imConversation.setType(0);// 0 单聊  1 群聊  2 系统消息

        imConversation.setUserIMId(mi_tencentId);
        imConversation.setUserId(mi_platformId);

        imConversation.setOtherPartyIMId(prom_custom_uid);
        imConversation.setOtherPartyId(user_id);

        imConversation.setUserName(mNickname);
        imConversation.setUserAvatar(mAvatar);

        imConversation.setOtherPartyName(mJoinLiveResponse.getLive_user().getUser_nickname());
        imConversation.setOtherPartyAvatar(CommonUtils.getUrl(mJoinLiveResponse.getLive_user().getAvatar()));

        imConversation.setConversationId(imConversation.getUserId() + "@@" + imConversation.getOtherPartyId());

        Intent intent = new Intent(mContext, MessageListActivity.class);
        intent.putExtra("IMConversation", imConversation);
        startActivity(intent);
    }

    //礼物
    private void openGiftWindow() {
        LiveGiftDialogFragment fragment = new LiveGiftDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("live_id", mLiveID);
        fragment.setArguments(bundle);
        fragment.show(getSupportFragmentManager(), "LiveGiftDialogFragment");
    }

    //关注主播更新状态
    public void addgz(String userid){
        if (userid.equals(mPusherId)){
            mFollowTv.setVisibility(View.GONE);
        }
    }

    //守护List
    public void openGuardListWindow() {
        LiveGuardDialogFragment fragment = new LiveGuardDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("liveUid", mPusherId);
        bundle.putInt("guardNum", mWatchNum);
        bundle.putString("isGuard", String.valueOf(mJoinLiveResponse.getViewer_user().getIs_watch()));
        fragment.setArguments(bundle);
        fragment.show(getSupportFragmentManager(), "LiveGuardDialogFragment");
    }

    //购买守护
    public void openBuyGuardWindow() {
        LiveGuardBuyDialogFragment fragment = new LiveGuardBuyDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("liveUid", mPusherId);
        bundle.putString("mLiveId", mLiveID);
        fragment.setArguments(bundle);
        fragment.show(getSupportFragmentManager(), "LiveGuardBuyDialogFragment");
    }

    //音浪充值
    public void getWallet(){
        Intent intent = new Intent(mContext, MyWalletActivity1.class);
        intent.putExtra("currentItem", 0);
        startActivity(intent);
        overridePendingTransition(R.anim.slide_right_entry, R.anim.hold);
    }

    //管理员
    public void manager(UserInfo4LiveResponse response){
        ManagerDialogFragment fragment = new ManagerDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("user_id", String.valueOf(response.getUser_info().getUser_id()));//用户ID
        bundle.putString("meUserId", mUserId);//自己ID
        bundle.putString("liveUid", mPusherId);//主播ID
        bundle.putString("mLiveId", mLiveID);//直播间ID
        bundle.putString("is_banspeech", String.valueOf(response.getUser_info().getIs_banspeech()));//是否被禁言
        bundle.putString("is_manager", String.valueOf(response.getUser_info().getIs_manager()));
        fragment.setArguments(bundle);
        fragment.show(getSupportFragmentManager(), "ManagerDialogFragment");
    }

    //日周月榜单
    public void contributeList(){
        ContributeListFragment fragment = new ContributeListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("live_id", mLiveID);
        fragment.setArguments(bundle);
        fragment.show(getSupportFragmentManager(), "ContributeListFragment");
    }

    //高级美颜回调
    @Override
    public void closeMain() {

    }

    @Override
    public void closeTieZhi(BeautyTieZhiFragment.BeautyParams params) {

    }

    @Override
    public void closeMeiYan(String proMeiFu, String proMeiXing) {

    }

    @Override
    public void closeLvJing(BeautyLvJingFragment.BeautyParams params) {

    }

    @Override
    public void onTrackingStatusChanged(int status) {
        //status <= 0  未检测到人脸
    }

    //收到提示消息
    @Override
    public void onLiveNotice(String message) {
        DialogUitl.showSimpleHintDialog(
                mContext,
                getString(R.string.prompt),
                getString(R.string.wszl_zhidao),
                getString(R.string.cancel),
                message,
                true,
                true,
                new DialogUitl.SimpleCallback2() {
                    @Override
                    public void onCancelClick() {
                    }
                    @Override
                    public void onConfirmClick(Dialog dialog, String content) {
                        dialog.dismiss();
                    }
                });
    }

    //收到退出直播间消息
    @Override
    public void closeLive(){
        showComfirmDialog();
    }

    //应用进入后台
    @Override
    public void onActivityPaused() {
        if (mLiveRoom != null){
            mLiveRoom.pausePush();
        }
    }

    //应用进入前台
    @Override
    public void onActivityResumed() {
        if (mLiveRoom != null){
            mLiveRoom.resumePush();
        }
    }

    //红包
    public void historyRP(String bonus_id, String avatar, String name){
        HistoryRPDialogFragment fragment = new HistoryRPDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("bonus_id", bonus_id);
        bundle.putString("avatar", avatar);
        bundle.putString("name", name);
        fragment.setArguments(bundle);
        fragment.show(getSupportFragmentManager(), "HistoryRPDialogFragment");
    }

    //红包
    public void RPResults(String bonus_id, String avatar, String name, String status, String money){
        RPResultsDialogFragment fragment = new RPResultsDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("bonus_id", bonus_id);
        bundle.putString("avatar", avatar);
        bundle.putString("name", name);
        bundle.putString("status", status);
        bundle.putString("money", money);
        fragment.setArguments(bundle);
        fragment.show(getSupportFragmentManager(), "RPResultsDialogFragment");
    }

    //###############################################################################################
    private int liveType = 1;
    private void startLivePlay() {
        liveType = mList.get(currentPosition).getType();
        if (liveType == 2){
            DialogUitl.showSimpleInputDialog(mContext,
                    "请输入房间密码",
                    null,
                    DialogUitl.INPUT_TYPE_NUMBER_PASSWORD,
                    8,
                    true,
                    false,
                    new DialogUitl.SimpleCallback2() {
                        @Override
                        public void onCancelClick() {
//                            exit();
                            errorP();
                        }
                        @Override
                        public void onConfirmClick(Dialog dialog, String content) {
                            if (TextUtils.isEmpty(content)) {
                                NToast.shortToast(mContext, "请输入房间密码");
                            } else {
                                startLivePlay_(content, dialog);
//                                dialog.dismiss();
                            }
                        }
                    });
        }else if (liveType == 3){

            DialogUitl.showSimpleHintDialog(mContext,
                    getString(R.string.prompt),
                    getString(R.string.dialog_reload),
                    getString(R.string.cancel),
                    "进入该直播间将收取" + mList.get(currentPosition).getIn_coin() + getString(R.string.my_jinbi)+"(1小时内退出再次进入不重复收取门票)",
                    true,
                    false,
                    new DialogUitl.SimpleCallback2() {
                        @Override
                        public void onCancelClick() {
//                            exit();
                            errorP();
                        }
                        @Override
                        public void onConfirmClick(Dialog dialog, String content) {
                            dialog.dismiss();
                            startLivePlay_(null, null);
                        }
                    });
        }else if (liveType == 4){
            int per_coin = mList.get(currentPosition).getPer_coin();
            DialogUitl.showSimpleHintDialog(mContext,
                    getString(R.string.prompt),
                    getString(R.string.dialog_reload),
                    getString(R.string.cancel),
                    "进入该直播间将收取" + per_coin + "音浪/分钟",
                    true,
                    false,
                    new DialogUitl.SimpleCallback2() {
                        @Override
                        public void onCancelClick() {
//                            exit();
                            errorP();
                        }
                        @Override
                        public void onConfirmClick(Dialog dialog, String content) {
                            dialog.dismiss();
                            startLivePlay_(null, null);
                        }
                    });
        }else {
            startLivePlay_(null, null);
        }
    }

    private void startLivePlay_(final String in_password, final Dialog dialog) {
        String url;
        if (vod_type == 2){
            url = "app/live/joinLive4Lubo";
        }else {
            url = "app/live/joinLive";
        }
        String body = "";
        try {
            body = new JsonBuilder()
                    .put("live_id", mLiveID)
                    .put("in_password", in_password)
                    .build();
        } catch (JSONException e) {
        }
        OKHttpUtils.getInstance().getRequest(url, body, new com.ningmengs789.live.Interface.RequestCallback() {
            @Override
            public void onError(int errCode, String errInfo) {
                if (in_password == null){
                    DialogUitl.showSimpleHintDialog(mContext,
                            getString(R.string.prompt),
                            getString(R.string.dialog_reload),
                            getString(R.string.cancel),
                            errInfo,
                            true,
                            false,
                            new DialogUitl.SimpleCallback2() {
                                @Override
                                public void onCancelClick() {
                                    exit();
                                }
                                @Override
                                public void onConfirmClick(Dialog dialog, String content) {
                                    dialog.dismiss();
                                    exit();
                                }
                            });
                }else {
                    NToast.shortToast(mContext, errInfo);
                }
            }
            @Override
            public void onSuccess(String result) {
                if (in_password != null && dialog != null){
                    dialog.dismiss();
                }

                if (result != null) {
                    try {
                        mJoinLiveResponse = JsonMananger.jsonToBean(result, JoinLiveResponse.class);
                        mPusherId = String.valueOf(mJoinLiveResponse.getLive_user().getUser_id());//房间创建者ID
                        mCurrentMemberCount = mJoinLiveResponse.getViewer_num();//观众数
                        mWatchNum = mJoinLiveResponse.getLive_user().getWatch_num();//守护数

                        pageChange();
                    } catch (HttpException e) {
                        e.printStackTrace();
                    }

                    if (liveType == 4){
                        setupTime();
                    }
                }
            }
        });
    }

    private class PagerAdapter extends android.support.v4.view.PagerAdapter {

        @Override
        public int getCount() {
            int size = mList.size();
            if (currentPosition == 0){
                if (size == 1){
                    return 1;
                }else {
                    return 2;
                }
            }else if (size == (currentPosition + 1)){
                return 2;
            }else {
                return 3;
            }
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = LayoutInflater.from(container.getContext()).inflate(R.layout.layout_portrait_live_item, null);
            view.setId(position);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(container.findViewById(position));
        }
    }

    private boolean isSliding = false;
    private int newItem;
    private void pageChange(){
        if (currentPosition == 0){
            newItem = 0;
        } else {
            newItem = 1;
        }
        mViewPager.setCurrentItem(newItem, false);
        ViewGroup currentView = (ViewGroup) mViewPager.getCurrentView();
        currentView.addView(mRootContainer);

        initData();
        startPlay();

        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            public void onPageSelected(int position) {
                if (position != newItem){
                    if (position > newItem){
                        currentPosition ++;
                    }else {
                        currentPosition --;
                    }
                    isSliding = true;
                    stopPlay();
                }
            }

            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private void errorP(){
        if (currentPosition == 0){
            newItem = 0;
        } else {
            newItem = 1;
        }
        mViewPager.setCurrentItem(newItem, false);

        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            public void onPageSelected(int position) {
                if (position != newItem){
                    if (position > newItem){
                        currentPosition ++;
                    }else {
                        currentPosition --;
                    }
                    isSliding = true;
                    exit();
                }
            }

            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    //###
    private Runnable updateTimeRunnable_2;
    private long time_2 = 0;
    private String order_no = "";
    private String duration_time;

    private void setupTime() {
        try {
            order_no = mJoinLiveResponse.getTime_live().getOrder_no();
            time_2 = 0;
            if (updateTimeRunnable_2 != null) {
                mHandler.removeCallbacks(updateTimeRunnable_2);
            }
            updateTimeRunnable_2 = new UpdateTimeRunnable2();
            mHandler.post(updateTimeRunnable_2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class UpdateTimeRunnable2 implements Runnable {

        public UpdateTimeRunnable2() {
        }

        @Override
        public void run() {
            time_2++;

            if (time_2 % 60 == 1){//每分钟一秒更新
                duration_time = String.valueOf((time_2 / 60) + 1);
                updTimeLiveDurationPerMin();
            }
            mHandler.postDelayed(this, 1000);
        }
    }

    private void updTimeLiveDurationPerMin(){
        String body = "";
        try {
            body = new JsonBuilder()
                    .put("order_no", order_no)
                    .put("duration_time", duration_time)
                    .build();
        } catch (JSONException e) {
        }
        OKHttpUtils.getInstance().getRequest("app/live/updTimeLiveDurationPerMin", body, new com.ningmengs789.live.Interface.RequestCallback() {
            @Override
            public void onError(int errCode, String errInfo) {
                Intent intent = new Intent(mContext, MyWalletActivity1.class);
                intent.putExtra("currentItem", 0);
                startActivity(intent);
                overridePendingTransition(R.anim.slide_right_entry, R.anim.hold);

                stopPlay();
            }
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject data = new JSONObject(result);
                    int rest_time = data.getInt("rest_time");

                    if (rest_time > 0 && rest_time < 3){
                        NToast.shortToast(mContext, "您好！剩余通話時長不足" + rest_time + "三分鐘！");
                    }

                    if (rest_time == 0){
                        Intent intent = new Intent(mContext, MyWalletActivity1.class);
                        intent.putExtra("currentItem", 0);
                        startActivity(intent);
                        overridePendingTransition(R.anim.slide_right_entry, R.anim.hold);

                        stopPlay();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
